package typetcp

import (
	"crypto/tls"
	"log"
	"net/http"
	"strings"
)

type BaseHTTP string

func (simple BaseHTTP) Brute() bool {
	// auth, err := ParseStringToBrute(string(simple))
	// if err != nil {
	// 	log.Println(err)
	// 	return false
	// }
	// Payload := []string{
	// 	"GET / HTTP/1.1",
	// 	"Host: $IP",
	// 	"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0",
	// 	"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
	// 	"Accept-Language: en-US,en;q=0.5",
	// 	"Accept-Encoding: gzip, deflate",
	// 	"Connection: close",
	// 	"Upgrade-Insecure-Requests: 1",
	// 	"Authorization: Basic $PWD",
	// }

	// sendPay := strings.Join(Payload, "\r\n") + "\r\n\r\n"
	// sendPay = strings.ReplaceAll(sendPay, "$IP", auth.Target)
	// pwd := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", auth.User, auth.Pwd)))
	// sendPay = strings.ReplaceAll(sendPay, "$PWD", pwd)
	// if !strings.Contains(auth.Target, ":") {
	// 	auth.Target += ":80"
	// }
	// if conn, err := net.Dial(auth.Target, "tcp"); err != nil {
	// 	log.Println(err)
	// 	return false
	// } else {
	// 	_, err := conn.Write([]byte(sendPay))
	// 	if err != nil {
	// 		log.Println(err)
	// 		return false
	// 	}
	// 	buf, _ := ioutil.ReadAll(conn)
	// 	if len(buf) > 0 {
	// 		result := bytes.SplitN(buf, []byte("\n"), 2)[0]
	// 		if !bytes.Contains(result, []byte("40")) {
	// 			return true
	// 		}

	// 	}
	// }
	// return false
	auth, err := ParseStringToBrute(string(simple))
	if err != nil {
		log.Println(err)
		return false
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}
	client := &http.Client{Transport: tr}

	/* Authenticate */
	req, err := http.NewRequest("GET", "http://"+auth.Target, nil)
	req.SetBasicAuth(auth.User, auth.Pwd)
	resp, err := client.Do(req)
	if err != nil {
		// fmt.Printf("Error : %s", err)
		return false
	}
	if resp.StatusCode%400 == 1 {
		return false
	} else if resp.StatusCode%200 == 1 {
		return true
	} else if resp.StatusCode%300 == 1 {
		return true
	}
	return false

}

type BaseHTTPS string

func (base BaseHTTPS) Brute() bool {
	auth, err := ParseStringToBrute(string(base))
	if err != nil {
		log.Println(err)
		return false
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}

	/* Authenticate */
	req, err := http.NewRequest("GET", "https://"+strings.TrimSpace(auth.Target), nil)
	req.SetBasicAuth(auth.User, auth.Pwd)
	resp, err := client.Do(req)
	if err != nil {
		// panic(err)
		// fmt.Printf("Error : %s|| %s\n", err, auth.Target)
		return false
	}
	if resp.StatusCode%400 == 1 {
		return false
	} else if resp.StatusCode%200 == 1 {
		return true
	} else if resp.StatusCode%300 == 1 {
		return true
	}
	return true
}
